﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tilver.Coder.CommandExcuteResult;
using Tilver.Coder.Core;
using Tilver.Coder.Utility;

namespace Tilver.Coder.Engine
{
    public class CommandEngine : EngineBase, ICommandEngine
    {
        public override string RenderToString()
        {
            var response = new StringBuilder();
            Response(response, Tabs);
            return response.ToString();
        }

        public CoderCommand Command { get; set; }

        private void Response(StringBuilder response, int tabs)
        {
            ResponseNameSpaces(response,tabs);
            response.AppendLine();
            response.AppendFormatLine(tabs, "namespace {0}", NameSpace);
            response.AppendTabLine(tabs, "{");

            response.AppendTabLine(tabs + 1, "#region EO");
            response.AppendLine();
            #region 输出实体类代码
            //只有返回结果是单行或多行时才生成实体类
            if (Command.CommandResult.CommandResult == ExecuteResultEnum.ExecuteSingle || Command.CommandResult.CommandResult == ExecuteResultEnum.ExecuteReader)
            {
                var eoEngine = new EOEngine<CoderCommand>();
                eoEngine.DBTextStyle = DBTextStyle;
                eoEngine.NameSpace = NameSpace;
                eoEngine.Tabs = tabs + 1;
                eoEngine.EO = Command;
                response.AppendLine(eoEngine.RenderToString());
            }
            #endregion
            response.AppendLine();
            response.AppendTabLine(tabs + 1, "#endregion");
            
            response.AppendTabLine(tabs + 1, "#region  PO");
            response.AppendLine();
            #region 输出操作类代码

            var poEngine = new CommandPOEngine();
            poEngine.DBTextStyle = DBTextStyle;
            poEngine.NameSpace = NameSpace;
            poEngine.Tabs = tabs + 1;
            poEngine.Command = Command;
            response.AppendLine(poEngine.RenderToString());
            
            #endregion

            response.AppendLine();
            response.AppendTabLine(tabs + 1, "#endregion");

            response.AppendTabLine(tabs, "}");
        }
    }
}
